/// Comparison functions for generic types.
/// These functions provide equality and ordering comparisons
/// for any type that implements `PartialEq` and `PartialOrd`.

/// Checks if two references are equal.
///
/// # Type Parameters
/// - `T`: The type of the elements being compared. Must implement `PartialEq`.
///
/// # Arguments
/// - `location1`: A reference to the first element.
/// - `location2`: A reference to the second element.
///
/// # Returns
/// - `true` if the elements are equal, `false` otherwise.
pub fn pointer_equal<T: PartialEq>(location1: &T, location2: &T) -> bool {
    location1 == location2
}

/// Compares two references and returns an ordering.
///
/// # Type Parameters
/// - `T`: The type of the elements being compared. Must implement `PartialOrd`.
///
/// # Arguments
/// - `location1`: A reference to the first element.
/// - `location2`: A reference to the second element.
///
/// # Returns
/// - `-1` if `location1` is less than `location2`.
/// - `1` if `location1` is greater than `location2`.
/// - `0` if `location1` is equal to `location2`.
pub fn pointer_compare<T: PartialOrd>(location1: &T, location2: &T) -> i32 {
    if location1 < location2 {
        -1
    } else if location1 > location2 {
        1
    } else {
        0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pointer_equal() {
        let a: i32 = 5;
        let b: i32 = 5;
        let c: i32 = 10;

        assert!(pointer_equal(&a, &b), "Expected equality for equal values");
        assert!(!pointer_equal(&a, &c), "Expected inequality for different values");
    }

    #[test]
    fn test_pointer_compare() {
        let a: i32 = 5;
        let b: i32 = 10;
        let c: i32 = 5;

        assert_eq!(pointer_compare(&a, &b), -1, "Expected -1 for a < b");
        assert_eq!(pointer_compare(&b, &a), 1, "Expected 1 for b > a");
        assert_eq!(pointer_compare(&a, &c), 0, "Expected 0 for equal values");
    }
}
